1. Field of the Invention
The present invention relates generally to computer software. More specifically, it relates to software for cleanly and forcibly terminating threads, or other minimal units of program execution, in a computer language execution environment.
2. Discussion of Related Art
Computer processes are made up of one or more threads. A thread is generally known as a minimal unit of execution in a computer system. When a program wants to stop a thread, the thread involved must be terminated in a safe and sure manner to prevent unstable states of data structures or objects, other processes, and of other entities in the computer system. Some operating systems (OSs) support termination of thread in a forcible manner. Some of them also support some form of clean up of the OS system state after thread termination. However, in some computer language execution environments like Java language execution system, the forcible thread termination supported by an underlying OS is not applicable, because this termination is not enough to support safe and clean termination of threads executing in the language execution system. The mechanism supported by the OS may ensure that the OS state is not unstablized, but it does not prevent unstable states of data structures in the language execution system. Threads are stopped in various ways. One of the more common ways is to stop a thread from a user program using a command. In Java language it is known as Thread.stop( ). The user program communicates with the system by issuing the command to terminate a thread. Using Thread.stop( ) the target thread can be terminated anywhere in the code of a program. This can result in a damaged and unstable language execution system status and can hinder clean thread termination.
For example, if a thread is terminated while a function is executing, such as the FileInputStream.read( ) function in the Java language execution environment, one or more data structures being used in the function are left in an unstable state using the current Thread.stop( ). The Thread.stop( ) command may abort operations on the data structures in the middle and unlock all the monitors that have locks on any data structures the thread is using so that other functions can use them. As a result, the data structures are left in an inconsistent state. This prevents any subsequent functions using them from proper execution. In addition, a ThreadDeath exception is propagated up the chain of functions from the function in which a thread is being terminated to the thread start up function. During this process, the exception can get caught in one of the functions and it will ignore the exception. If the exception is not propagated, the thread that is supposed to be terminated may not stop. Moreover, the finally clause in a function is executed whenever the function is exited. Thus, during the propagation of the ThreadDeath exception, the finally clause will execute. The finally clause can include such an operation that keeps the thread running, thereby preventing the clean and certain termination of a thread.
In such a program language execution system that depends on the finally clause mechanism for cleanly unlocking monitors, if execution of the finally clause is ignored, any remaining locked monitors maybe kept locked; there is no guarantee that the objects locked will be unlocked. It is the completion of the finally clause that unlocks any remaining monitors locked in that function. This uncertainty also prevents clean thread termination since monitors may remain locked or unlocked. Generally, all the code in a finally clause should normally be executed when exiting a function. The finally clause should complete in a normal manner. If ignored, the monitors will remain locked, since the finally clause is responsible for unlocking resources when a process completes. If the data structures remain locked (i.e., the monitors keep their locks), other threads cannot access the data structures, which has other unwanted consequences.
In addition, another function known as Thread.interrupt( ) does not guarantee the clean and certain termination of a thread and cannot be used to arbitrarily terminate a thread from a user program.
Therefore, it would be desirable to have a method of cleanly and forcibly terminating a thread in a computer language execution environment. Such a method should leave no uncertainty about whether the thread has terminated and should not leave objects in the system in an unstable state or states.